package com.czy.utilslib.photo;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.ImageView;

import com.czy.utilslib.image.ImageUtil;

/**
 * Content Provider选择照片，获取Uri，通过Uri转化为Bitmap（ImageUtils）
 */
public class SelectPhotoUtil {

    public static int SELECT_IMAGE_CODE = 211;

    @SuppressLint("IntentReset")
    public void selectImageFromAlbum(Activity activity){
        try {
            Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            intent.setType("image/*");
            activity.startActivityForResult(intent,SELECT_IMAGE_CODE);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private Uri imagUri;

    public Uri handleImageResult(int requestCode, int resultCode, Intent data,Activity activity, ImageView imageView, ImageUtil imageUtil){
        if(resultCode == Activity.RESULT_OK){
            if(requestCode == SELECT_IMAGE_CODE){
                if(data != null){
                    imagUri = data.getData();
                    Bitmap bitmap = imageUtil.uriToBitmapMediaStore(activity,imagUri);
                    if(bitmap != null){
                        imageView.setImageBitmap(bitmap);
                        return imagUri;
                    }
                }
            }
        }
        return null;
    }

}


